<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('menu_item_normal_form.class.php');
require_once('menu_item_normal_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/item_types/tcms_menu_item_normal_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');


class Menu_Item_Normal_Form_Ctrl extends GForm_Ctrl_With_Object {
	protected function create_form() {
		$form = new menu_item_normal_form();

		$dbc = $this->get_dbc();

//		$form->generate_html();

		return $form;
	}


	protected function create_form_view() {
		$ret = new Menu_Item_Normal_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;


		return $ret;
	}


	protected function on_bad_submit() {

	}


	protected function create_object() {
		$object = new TCMS_Menu_Item_Normal_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('url_value', $object->get_url_value());
		$form->set_field_value('label', $object->get_label());
		$form->set_field_value('ordering', $object->get_ordering());
		$form->set_field_value('nofollow', $object->get_nofollow());
		$form->set_field_value('new_window', $object->get_new_window());
		$form->set_field_value('additional', $object->get_additional());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_url_value($form->get_field_value('url_value'));
		$object->set_label($form->get_field_value('label'));

		if ($form->get_field_value('ordering')) {
			$object->set_ordering($form->get_field_value('ordering'));
		}

		$object->set_nofollow($form->get_field_value('nofollow'));
		$object->set_new_window($form->get_field_value('new_window'));
		$object->set_additional($form->get_field_value('additional'));

		if ($this->get_permanent_external_param('add_new', true)) {
			$object->set_type($this->get_permanent_external_param('type'));
			$object->set_menu($this->get_permanent_external_param('menu'));
			$object->set_parent($this->get_permanent_external_param('parent'));
		}
	}


	public function prepare_order($parent, $menu_id) {
		$dbc = $this->get_dbc();

		$next_ordering = TCMS_menu_item_normal_DBC::get_next_order($dbc, $parent, $menu_id);

		$form = $this->get_form();
		$object = $this->get_object();
		$object->set_ordering($next_ordering);
	}


	protected function on_trans_ending(DB_Connection $dbc) {
		$object = $this->get_object();
		TCMS_Menu_DBC::menu_changed($dbc, $object->get_menu(), $this->get_permanent_external_param('user_id'));
	}
}